package com.jy.adscm.operation.newhscrb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.newhscrb.bean.NhcMachine;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcMachine;
import com.jy.adscm.operation.newhscrb.mapper.mysql.NhcMachineMapper;
import com.jy.adscm.operation.newhscrb.service.NhcMachineService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 耗材生产机器基本信息 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-06-18
 */
@Service
public class NhcMachineServiceImpl extends ServiceImpl<NhcMachineMapper, NhcMachine> implements NhcMachineService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Override
    public boolean insertNhcMachine(NhcMachine nhcMachine) {
        //生成二维码信息 年+月+日 +序号
        String date =new SimpleDateFormat("yyyyMMdd").format(new Date());
        Wrapper<NhcMachine> wrapper=new EntityWrapper<>();
        wrapper.like("code",date, SqlLike.RIGHT);
        wrapper.orderBy("code",false);
        List<NhcMachine> nhcMachines = super.selectList(wrapper);
        String code="";
        if (nhcMachines!=null&&nhcMachines.size()>0){
            NhcMachine machine = nhcMachines.get(0);
            code = String.valueOf(Long.valueOf(machine.getCode())+1) ;
        }else {
            code=date+"01";
        }
        nhcMachine.setCode(code);
        boolean insert = super.insert(nhcMachine);
        return insert;
    }

    @Override
    public boolean updateNhcMachine(NhcMachine nhcMachine) {
        boolean b = super.updateById(nhcMachine);
        return b;
    }

    @Override
    public Page<NhcMachine> select(QueryNhcMachine queryNhcMachine) {
        Page<NhcMachine> page=new Page<>(queryNhcMachine.getCurrentPage(),queryNhcMachine.getPageSize());
        Wrapper<NhcMachine> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryNhcMachine.getName(),null)){
            wrapper.like("name",queryNhcMachine.getName());
        }
        page=  super.selectPage(page,wrapper);
        //口罩生产工序
        List<JyCode> gxList = jyCodeMapper.getJyCodeByCodeType("MASK_GX");
        Map<String ,String> gxMap = new HashMap<>();
        for (JyCode jyCode : gxList) {
            gxMap.put(jyCode.getCode(),jyCode.getName());
        }
        String date = queryNhcMachine.getDate();
        if (!AdscmBaseUtils.getInstance().isNotEmpty(date,null)){
            date = AdscmDateUtils.getNowOnlyDateFomat1(new Date());
        }

        List<NhcMachine> records = page.getRecords();
        for (NhcMachine record : records) {
            if (AdscmBaseUtils.getInstance().isNotEmpty( record.getCreator() ,null)){
                JyUser jyUser = jyUserMapper.selectById(record.getCreator());
                record.setCreatorName(jyUser.getName());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public List<NhcMachine> selectByCode(String code) {
        Wrapper<NhcMachine> wrapper=new EntityWrapper<>();
        wrapper.eq("code",code);
        List<NhcMachine> nhcMachines = selectList(wrapper);
        return nhcMachines;
    }
}
